﻿using HtmlAgilityPack;
using MyCrawlerDemo;
using SqlSugar;
using System.Configuration;

Console.WriteLine("Hello, World!");

try {
    #region 声明服务
    var sqltype = ConfigurationManager.AppSettings["SqlType"]?.ToString();
    ConnectionConfig config = new()
    {
        ConnectionString = "Data Source=D:\\WorkSpace\\dotnetadvanced\\爬虫\\MyCrawlerDemo\\PracticeDb.db",
        DbType = sqltype switch
        {
            "sqlite" => DbType.Sqlite,
            "sqlserver" => DbType.SqlServer,
            _ => throw new Exception("未知的数据库类型")
        },
        IsAutoCloseConnection = true
    };
    CrawlerService service = new(config);
    #endregion

    #region 数据库初始化
    //var initResult = service.Init();
    //Console.WriteLine($"数据库初始化:{initResult}");
    #endregion

    #region 获取所有分类
    //var saveResult = service.GetCategoriesAndSave(ConfigurationManager.AppSettings["AllSortUrl"].ToString());
    //Console.WriteLine($"保存数据: {saveResult}");
    #endregion
//https://list.jd.com/list.html?cat=1315%2C13529%2C13530&stock=1&isList=1&page=3&s=60&click=0&log_id=1744254707285.3957
    // TODO: 爬取每个分类下的商品信息并保存到数据库中
    using SqlSugarClient client = new(service.Config);
    int pagenumber = 1;
    int pageSize = 100;
    int total = 0;
    List<JdCategory>? categories;
    var cookie = ConfigurationManager.AppSettings["Cookie"]!.ToString();
    List<JdCommodity> commodities = new();
    do
    {
        categories = client.Queryable<JdCategory>().Where(l => !string.IsNullOrEmpty(l.Url) && l.Url.StartsWith("//list.jd.com")).ToPageList(pagenumber, pageSize, ref total);
        if (categories != null && categories.Count > 0)
        {
            foreach (JdCategory category in categories)
            {
                string baseUrl = $"https:{category.Url}";
                Console.WriteLine(baseUrl);
                break;
            }
        }
        break;
    }
    while (pagenumber++ * pageSize <= total);
}
catch (Exception ex) {
    Console.WriteLine(ex.Message);
}
